module break_led(   
    input                       clk,
    input                       rst_n,
    output   reg   [3:0]        led         
);

localparam     DELAY_2US    =       'd99;
localparam     DELAY_2MS    =       'd999;
localparam     DELAY_2S     =       'd999;

reg [6:0]      cnt_2us ;
reg [9:0]      cnt_2ms ;
reg [9:0]      cnt_2s  ;
reg            flag_2s ;


always @ (posedge sclk or negedge s_rst_n)
begin
    if(s_rst_n == 1'b0)
        cnt_2us <=  'd0;
    else if (cnt_2us == DELAY_2US)
        cnt_2us <=  'd0; 
    else  
        cnt_2us <= cnt_2us + 1'b1;
end

always @ (posedge sclk or negedge s_rst_n)
begin
    if(s_rst_n == 1'b0)
        cnt_2ms <=  'd0;
    else if (cnt_2us == DELAY_2US && cnt_2us == DELAY_2MS)	   //微秒和毫秒都同时达到最大值，否则毫秒计数到99时只用了一个周期
        cnt_2ms <=  'd0; 
    else if (cnt_2ms == DELAY_2MS)
        cnt_2ms <= cnt_2ms + 1'b1;
end

always @ (posedge sclk or negedge s_rst_n)
begin
    if(s_rst_n == 1'b0)
        cnt_2s <=  'd0;
    else if (cnt_2us == DELAY_2US && cnt_2ms == DELAY_2MS && cnt_2s == DELAY_2S)
        cnt_2s <=  'd0; 
    else if(cnt_2s == DELAY_2S )
        cnt_2s <= cnt_2s + 1'b1;
end

always @ (posedge sclk or negedge s_rst_n)
begin
    if(s_rst_n == 1'b0)
        led <= 4'b1111;
    else if(cnt_2ms <= cnt_2s && flag_2s <= 1'b0)          //逐渐变亮
        led <= 4'b0000;
    else if(cnt_2ms >= cnt_2s && flag_2s <= 1'b1)                             //逐渐变暗
        led <= 4'b0000;
    else 
        led <= 4'b111 1;       
end

always @ (posedge sclk or negedge s_rst_n)
begin
    if(s_rst_n == 1'b0)
        flag_2s <= 1'b0;
    else if(cnt_2us == DELAY_2US && cnt_2ms == DELAY_2MS && cnt_2s == DELAY_2S)
        flag_2s <= ~flag_2s;
end

endmodule